/*
 * @Date: 2023-03-06 13:42:55
 * @LastEditTime: 2023-08-07 16:06:38
 * @FilePath: /wkw/src/router/index.ts
 * 介绍:路由文件
 */

import {
  createRouter,
  // createWebHistory,
  createWebHashHistory,
  RouteRecordRaw,
} from "vue-router"; //引入路由
import { importRouterModules, autoImportRoutes } from "./importGlob";
import useUserinfoStore from "@@/store/useUserinfoStore";
import { getCurrentHrefQueryParam } from "@@/utils/tools";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
NProgress.configure({
  easing: "ease", // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3, // 初始化时的最小百分比
});

const routes: Array<RouteRecordRaw> = [];

routes.push(...autoImportRoutes, ...importRouterModules());

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

//解析守卫
// router.beforeResolve(async (to) => {
//   return true;
// });

//前置路由守卫
router.beforeEach(async (to, from, next) => {
  NProgress.start();
  const query = getCurrentHrefQueryParam();
  const userinfoStore = useUserinfoStore();
  const whiteList = ["/"];
  if (whiteList.includes(to.path as string)) return next();
  if (!window.isLogined) {
    userinfoStore.login(query);
    return next("/");
  }
  next();
});

//路由跳转错误
router.onError((error) => {
  NProgress.done();
  console.warn("路由错误", error.message);
});

//路由跳转结束
router.afterEach(() => {
  NProgress.done();
});

export default router;
